package org.jeecg.modules.demo.ceshilist.service.impl;

import org.jeecg.modules.demo.ceshilist.entity.Ceshi;
import org.jeecg.modules.demo.ceshilist.entity.Ceshilist;
import org.jeecg.modules.demo.ceshilist.mapper.CeshilistMapper;
import org.jeecg.modules.demo.ceshilist.mapper.CeshiMapper;
import org.jeecg.modules.demo.ceshilist.service.ICeshiService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.util.List;
import java.util.Collection;

/**
 * @Description: 测试表
 * @Author: jeecg-boot
 * @Date:   2021-02-05
 * @Version: V1.0
 */
@Service
public class CeshiServiceImpl extends ServiceImpl<CeshiMapper, Ceshi> implements ICeshiService {

	@Autowired
	private CeshiMapper ceshiMapper;
	@Autowired
	private CeshilistMapper ceshilistMapper;
	
	@Override
	@Transactional
	public void saveMain(Ceshi ceshi, List<Ceshilist> ceshilistList) {
		ceshiMapper.insert(ceshi);
		if(ceshilistList!=null && ceshilistList.size()>0) {
			for(Ceshilist entity:ceshilistList) {
				//外键设置
				entity.setMainid(ceshi.getId());
				ceshilistMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional
	public void updateMain(Ceshi ceshi,List<Ceshilist> ceshilistList) {
		ceshiMapper.updateById(ceshi);
		
		//1.先删除子表数据
		ceshilistMapper.deleteByMainId(ceshi.getId());
		
		//2.子表数据重新插入
		if(ceshilistList!=null && ceshilistList.size()>0) {
			for(Ceshilist entity:ceshilistList) {
				//外键设置
				entity.setMainid(ceshi.getId());
				ceshilistMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional
	public void delMain(String id) {
		ceshilistMapper.deleteByMainId(id);
		ceshiMapper.deleteById(id);
	}

	@Override
	@Transactional
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			ceshilistMapper.deleteByMainId(id.toString());
			ceshiMapper.deleteById(id);
		}
	}
	
}
